import json
import deepforge
import signal
import sys
import os

def signal_handler(signum, frame):
    sig = signal.Signals(signum)
    print('Received ' + sig.name + '. Exiting.')
    sys.exit(1)

signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)

<%= initCode %>
# input data<% inputs.forEach(function(pair) { var input = pair[0], type = pair[1], isNil = pair[2];%>
<%= input %> = <% if (isNil) { %>None<% } else { %>deepforge.serialization.load('<%= type %>', open('./artifacts/<%= input %>', 'rb')) <%}}); %>

<% references.forEach(function(pair) {
    var name = pair[0], code = pair[1];
    %>############ Create the <%= name %> ############
<%= code %>
<%= name %> = result
############ <%= name %> created ############<%}); %>

from operations import <%= name %>

# main operation code for <%= name %>
operation = <%= name %>(<%= arguments %>)
print('\n############### Running "<%= name.replace(/'/g, '\\\'') %>" Operation ############### ')
<%= outputs.length ? outputs.join(', ') : 'result' %> = operation.execute(<%= inputs.map(function(pair) {
    var name = pair[0],
        isNone = pair[2];
    return isNone ? 'None' : name;
})%>)
print('############### "<%= name.replace(/'/g, '\\\'') %>" Operation Complete! ###############')

<% outputs.forEach(name => { %>
os.makedirs('outputs/<%= name %>/', exist_ok=True)
with open('outputs/<%= name %>/metadata.json', 'w') as outfile:
    metadata = {}
    metadata['type'] = deepforge.serialization.get_full_class_name(<%= name %>)
    json.dump(metadata, outfile)

with open('outputs/<%= name %>/data', 'wb') as outfile:
    deepforge.serialization.dump(<%= name %>, outfile)
<% }); %>
